package SteamAPI;

import LambdaExpress.Sort;
import LambdaExpress.Student;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 已知，有一个List<Student>的集合
 * 实现： 提取出所有二年级的学生
 * 将二年级的学生按照年龄倒序排序
 * 将排序后的学生转换为 {姓名-年龄-年级},已逗号分隔的方式拼接为一个字符串并返回
 * 输出得到的字符串
 */
public class Demo {
    public static void main(String[] args) {
      //Convention();//常规的方法
      SteamMethod();//steam方法过滤
    }

    public static void Convention()/*常规方法完成*/{
        Student[] stu= Sort.getStudents();
        List<Student> list=new ArrayList<>();
        /*筛选二年级学生*/
        for (Student student : stu) {
          if("二年级".equals(student.getGrade())){
              list.add(student);
          }
        }

        Collections.sort(list,(o1,o2) ->{return o1.getAge()- o1.getAge();});
        StringJoiner str=new StringJoiner("\n-->");
        for (Student s : list) {
            StringJoiner stringJoiner=new StringJoiner("-","{","}");
            stringJoiner.add(s.getName());
            stringJoiner.add(Integer.toString(s.getAge()));
            stringJoiner.add(s.getGrade());
            str.add(stringJoiner.toString());//stringJoiner转字符串
        }

        System.out.println("-->"+str.toString());
    }

    public static void SteamMethod(){
        List<Student> stu =Sort.getStudentsList();
        String str=stu.stream()
                .filter(student ->{return "二年级".equals(student.getGrade());})
                .sorted((o1,o2) ->{return o1.getAge()- o2.getAge();})
                .map(stu1 -> {
                  // 将student对象转换为 {姓名-年龄-年级}
                   StringJoiner join=new StringJoiner("-","{","}");
                   join.add(stu1.getName());
                   join.add(Integer.toString(stu1.getAge()));
                   join.add(stu1.getGrade());
                   return join.toString();
                })
                .collect(Collectors.joining(","));

        System.out.println(str);

    }
}
